package cn.zy.framework.helper;

import cn.zy.framework.annotation.Controller;
import cn.zy.framework.annotation.Service;
import cn.zy.framework.util.ClassUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: Administrator
 * @Description:
 * @Date: Created in 16:58 2021/9/10
 * @Modified By:
 * @version: 1.0
 * 类操作助手类
 */
public final class ClassHelper {

	private static final Set<Class<?>> CLASS_SET;

	static {
		String basePackage = ConfigHelper.getAppBasePackage();
		CLASS_SET = ClassUtil.getClassSet(basePackage);
	}

	/**
	 * 获取应用包名下的所有类
	 * @return
	 */
	public static Set<Class<?>> getClassSet(){
		return CLASS_SET;
	}

	/**
	 * 获取应用包名下的所有 Service 类
	 * @return
	 */
	public static Set<Class<?>> getServiceClassSet(){
		Set<Class<?>> classSet = new HashSet<Class<?>>();
		for (Class<?> cls : CLASS_SET) {
			if (cls.isAnnotationPresent(Service.class)){
				classSet.add(cls);
			}
		}
		return classSet;
	}

	/**
	 * 获取应用包名下的所有 Controller 类
	 * @return
	 */
	public static Set<Class<?>> getControllerClassSet(){
		Set<Class<?>> classSet = new HashSet<Class<?>>();
		for (Class<?> cls : CLASS_SET) {
			if (cls.isAnnotationPresent(Controller.class)){
				classSet.add(cls);
			}
		}
		return classSet;
	}

	/**
	 * 获取应用包名下的所有 Bean 类 (包括：Service、Controller 等)
	 * @return
	 */
	public static Set<Class<?>> getBeanClassSet(){
		Set<Class<?>> classSet = new HashSet<Class<?>>();
		classSet.addAll(getServiceClassSet());
		classSet.addAll(getControllerClassSet());
		return classSet;
	}

}
